$(function(){
    /**
     * 分页初始条件
     */
    var total_count = 1;//分页总记录数
    var total = 1;//分页总页面数
    var currentPage = 1;//当前页
    var pageSize = pageOption.pageSize;//每页显示的记录数
    var idList = [];//批量选择id所存的数组
    var root_url = $("#root_url").val();
    var phone_list = $('.phone-list').val();
    var uids = $('.uids').val();

    /**
     * 页面初始化
     */
    function init(){
        myPagination();
        bindEvent();
    }

    /**
     * 事件绑定
     */
    function bindEvent(){
        var index = parent.layer.getFrameIndex(window.name);
        //根据条件查询商品信息
        $('.inner-section .search-btn').click(function() {
            projectsXm.closed();
            render(true,1,pageSize);
            return false;
        });
        //enter键盘事件
        $(".inner-section .search-param-form input").keydown(function(event){
            event = event ? event: window.event;
            if(event.keyCode == 13){
                render(true,1,pageSize);
                return false;
            }
        });
    }
    /**
     * 获取模糊参数
     */
    function getSelectInfo(){
        var selectInfo = {
            ids:$('#ids').val(),
            order_id:$('#order_id').val()
        };
        return selectInfo;
    }
    /**
     * 分页显示方法
     */
    function myPagination(){
        render(true,1,pageSize);
        //调用公共分页方法
        pagination("#page-selection",{pageSize:pageSize,total:total},render);
    }
    /**
     * 分页动态渲染数据
     * @param async ajax请求是否异步
     * @param pageIndex 当前显示页
     * @param pageSize 每页显示记录数
     */
    function render(async,pageIndex,pageSize){
        var selectInfo = getSelectInfo();
        selectInfo.pageIndex = pageIndex;
        selectInfo.pageSize = pageSize;
        $.ajax({
            async:async,
            type:'post',
            url:'pagingStoreList',
            data:selectInfo,//从1开始计数
            dataType:'json',
            success:function(result){
                var html ='';
                if(result.errorCode == 0){
                    total = result.data.pageInfo.total_page;
                    total_count = result.data.pageInfo.total_count;
                    $("#page-selection").bootpag({total:total,total_count:total_count});//重新计算总页数,总记录数

                    currentPage = result.data.pageInfo.current_page;
                    var userList = result.data.dataList;

                    html+='<tr>' +
                        '<th class="" style="width: 80px;"></th>' +
                        '<th class="">序号</th>' +
                        '<th class="">门店名称</th>' +
                        '<th class="">所在城市</th>' +
                        '<th class="">详细地址</th>' +
                        '<th class="">距离(公里)</th>' +
                        '</tr>';
                    var colspan = $(html).find("th").length;
                    for(var i = 0; i < userList.length;i++){
                        var obj = userList[i];
                        var num = (pageIndex - 1) * pageSize + i + 1;
                        var user_id = obj.user_id;
                        var memberUsername = obj.memberUsername || '--';
                        var title = obj.title;
                        var city = obj.city;
                        var address = obj.address;
                        var add_time = obj.add_time;
                        var bind_time = obj.bind_time;
                        var state = obj.state;
                        var distance = obj.distance;
                        var id = obj.id;
                        var checked = (idList.indexOf(id.toString())>=0) ? "checked":"";//判断当前记录先前有没有被选中

                        html+='<tr>'
                            +'<td><input type="radio" name="id" class="select-single" value="'+id+'" '+checked+'></td>'
                            +'<td>'+num+'</td>'
                            + '<td class="">' + title + '</td>'
                            + '<td class="">' + city + '</td>'
                            + '<td class="">' + address + '</td>'
                            + '<td class="">' + distance + '</td>'
                    }
                    if(userList.length == 0){
                        html += '<tr><td colspan="'+colspan+'"><p class="text-danger">暂无数据。</p></td></tr>';
                    }
                    $(".inner-section #list-table tbody").html(html);
                    //选择
                    $('.select-single').click(function () {
                        if ($(this).prop('checked')) {
                            idList = $(this).val();
                        }
                    })
                }else{
                    responseTip(result.errorCode,json.errorInfo,1500);
                }

            },
            error:errorResponse
        });
    }

    getIdList = function(){
        return {
            id: idList[0] || -1,
        }
    }

    init();
});
function getData(){
    return getIdList() ;
}